home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 17959 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.3 KB  |  72 lines

  1. Path: lf.hp.com!gajdos
  2. From: gajdos@lf.hp.com (Larry Gajdos)
  3. Newsgroups: comp.lang.c++
  4. Subject: STL with VC 4.1 and namespaces
  5. Date: 17 Apr 1996 15:30:20 GMT
  6. Organization: Hewlett-Packard Little Falls Site
  7. Message-ID: <4l32qc$l6v@hpavua.lf.hp.com>
  8. NNTP-Posting-Host: gajdos@eden.lf.hp.com
  9. X-Newsreader: TIN [version 1.2 PL2.2]
  10.  
  11. Here is yet another problem with the namespace "patch" suggested by
  12. Microsoft for using the STL with the foundation classes: The following
  13. compiles successfully (and the logic works in the larger context from
  14. which this was extracted) when the namespace patch is not used; 
  15. however, it gives a compiler error when using the namespace patch.
  16. The code:
  17.  
  18. ////////////////////////////////////////////////////////////////
  19. #pragma warning( disable:4786 )  // debugger symbol too large (>255 char)
  20. #include <new.h>
  21. namespace std  {
  22. #include <bool.h>
  23. #include <bstring.h>
  24. #include <map.h>
  25. }
  26. using namespace std;
  27.  
  28. class A;
  29. typedef string (A::*pFunc)() const;
  30. typedef map< string, pFunc, less<string> > FUNCMAP;
  31.  
  32. class A  {
  33. public:
  34.     A();
  35.     string HelloFunc() const  {return "Hello!";}
  36. private:
  37.     FUNCMAP* m_pMap;
  38.     FUNCMAP::iterator m_iMap;
  39. };
  40. ////////////////////////////////////////////////////////////////
  41.  
  42. The program defines a class A containing a pointer to a map of strings
  43. (defined using the bstring.h file included with the STL) and pointers
  44. to A member functions returning strings. The declaration of the iterator
  45. (on the second last line) leads to the following compiler errors being
  46. reported for the default construct line in <pair.h>:
  47.  
  48. pair() : first(T1()), second(T2()) {}
  49. C2440: 'initializing' cannot convert from 'class std::basic_string<char>' to
  50.                'class std::basic_string<char> (A::*)(void)const'
  51. C2439: 'second': member could not be initialized
  52.  
  53. Well, T2 in this case is the function pointer, so this kind of makes sense.
  54. But everything compiles and works _without_ the namespaces...
  55.  
  56. I have found a workaround. If I redefine the pFunc typdef to refer to A
  57. members with no (void) return, then everything compiles:
  58.  
  59. typedef void (A::*pFunc)()const;
  60.  
  61. With this approach, I now need to cast assignments to the map's second
  62. element, for example,
  63.  
  64. (*m_pMap)[ "Hello" ] = (pFunc)HelloFunc;
  65.  
  66. I guess I can live with this.
  67.  
  68. I would appreciate any comments!!
  69.  
  70. Larry Gajdos
  71. not speaking for HP
  72.